package com.xxx.api.controller.user;


import com.xxx.common.core.dto.ApiResult;
import com.xxx.common.core.dto.JsonResult;
import com.xxx.common.core.log.AppLog;
import com.xxx.common.core.log.AppLogFactory;
import com.xxx.common.core.sampleframe.search.Search;
import com.xxx.service.user.entity.UserBase.UserBase;
import com.xxx.service.user.service.UserBase.UserBaseService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.geo.Point;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import java.util.*;


@Controller
@RequestMapping("/api/user")
public class UserBaseController {

	private static final AppLog logger = AppLogFactory.getLog(UserBaseController.class);

	private static Map<String,Long> userLoginState = new HashMap<String,Long>();
	@Autowired
	private UserBaseService userBaseService;

	@RequestMapping("/register")
	@ResponseBody
	public String register(String loginName, String password){
		UserBase userBase = new UserBase();
		userBase.setLoginName(loginName);
		userBase.setPassword(password);
		userBase.setName("");
		userBaseService.insert(userBase);
		return JsonResult.success("ok");
	}

	@RequestMapping("/testTransaction")
	@ResponseBody
	public String testTransaction(){
		userBaseService.transcation();
		return JsonResult.success("ok");
	}
	@RequestMapping("/update")
	@ResponseBody
	public String update(String loginName, String password){
		UserBase userBase = new UserBase();
		userBase.setLoginName(loginName);
		userBase.setPassword(password);
		userBase.setName("");
		userBaseService.insert(userBase);
		return JsonResult.success("ok");
	}



	@RequestMapping("/login")
	@ResponseBody
	public String login(String loginName, String password) {
		UserBase userBase = userBaseService.findOne(new Search().addSearchFilter("EQ_loginName",loginName));
		if(userBase.getPassword().equals(password)){
			String token = UUID.randomUUID().toString();
			userLoginState.put(token,userBase.getId());
			return ApiResult.success("登陆成功",token);
		}
		return JsonResult.success("ok");
	}

	@RequestMapping("/{id}")
	@ResponseBody
	public String findById(@PathVariable Long id) {
		return JsonResult.success("查询成功",userBaseService.findById(id));
	}




}
